/**
 * @author Kostya
 */

var iWindow = null;
var iStatusBar = null;

var iOpener = null;

Ext.onReady(function(){
	iOpener = Ext.air.NativeWindow.getRootHtmlWindow();
	Ext.QuickTips.init();
	
	iWindow = window.nativeWindow;
	
	iStatusBar = new Ext.StatusBar({
		defaultText: 'Idle',
		region: 'south'
	});
	
	iNetworkListener = function(aNetwork){
		lGoButton.setDisabled(aNetwork);
	}
	
	var lKeyHandler = function(item, evt){
//		if(evt.getKey()==Ext.EventObject.TAB){
//			evt.stopEvent();
//			for(var i = 0; i<lFocusArray.length; i++){
//				if(lFocusArray[i]==item){
//					if(evt.shiftKey){
//						if(i>0)
//							lFocusArray[i-1].focus(false, 10);
//						else
//							lFocusArray[lFocusArray.length-1].focus(false, 10);
//					}else{
//						if(i==lFocusArray.length-1)
//							lFocusArray[0].focus(false, 10);
//						else
//							lFocusArray[i+1].focus(false, 10);
//					}
////					return false;
//				}
//			}			
//		}		
	};
	
	var lServerText = new Ext.form.TextField({
		fieldLabel: 'Server',
		allowBlank: true,
		msgTarget: 'under',
		value: 'beorganized.org'
	});
	
	lServerText.on('keydown', lKeyHandler);
	
	var lLoginText = new Ext.form.TextField({
		fieldLabel: 'User name',
		allowBlank: true,
		enableKeyEvents: true,
		msgTarget: 'under',
		value: ''
	});
	
	lLoginText.on('keydown', lKeyHandler);
	
	lLoginText.on('keydown', function(item, evt){
		if(evt.getKey()==Ext.EventObject.ENTER){
			lPasswordText.focus(false, 10);
		}
	});
	
	var lPasswordText = new Ext.form.TextField({
		fieldLabel: 'Password',
		enableKeyEvents: true,
		allowBlank: true,
		msgTarget: 'under',
		inputType: 'password',
		value: ''
	});
		
	lPasswordText.on('keydown', lKeyHandler);
	lPasswordText.on('keydown', function(item, evt){
		if(evt.getKey()==Ext.EventObject.ENTER){
			lGoHandler();
		}
	});
		
	var lSavePassword = new Ext.form.Checkbox({
		enableKeyEvents: true,
		fieldLabel: 'Save password'
	});
	
	lSavePassword.on('keydown', lKeyHandler);
	
	var lGoHandler = function(){
		air.trace('Submitting...');
		var password = hex_md5(lPasswordText.getValue());
		callAPI(lServerText.getValue(), 'user.CheckUserNamePassword', {
			userName: lLoginText.getValue(),
			password: password
		}, function(aXML){
			air.trace('token: '+aXML.documentElement.getAttribute('token'));
			iOpener.newSessionCreated(lServerText.getValue(), lLoginText.getValue(), 
					aXML.documentElement, lSavePassword.getValue());
			iWindow.close();
		}, function(){
			lPasswordText.setValue('');
			lPasswordText.focus();
		});
	}
	
	var lGoButton = new Ext.Button({
		enableKeyEvents: true,
		text: 'Login',
		handler: lGoHandler
	});
	
	lGoButton.on('keydown', lKeyHandler);
	
	var lFocusArray = [lServerText, lLoginText, lPasswordText, lSavePassword, lGoButton];
	
	
	var lSessionForm = new Ext.form.FormPanel({
		frame: true,
		region: 'center',
		minButtonWidth: 100,
		labelAlign: 'right',
		defaults: {
			anchor: '100%'
		},
		items:[lServerText, lLoginText, lPasswordText, lSavePassword],
		buttons:[lGoButton]
	});
	
	var lViewport = new Ext.Viewport({
		layout: 'border',
		items: [lSessionForm, iStatusBar]
	});
	
	iWindow.visible = true;
	iWindow.activate();
	lViewport.doLayout();
	lLoginText.focus();
});
